﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Tetris.Grid;

namespace Tetris.Blocks
{
    public class Block
    {
        private Texture2D m_texture;
        public Texture2D texture
        {
            get
            {
                return m_texture;
            }
        }

        private GridPosition m_gridPosition;
        public GridPosition gridPosition
        {
            get
            {
                return m_gridPosition;
            }
            set
            {
                m_gridPosition = value;
            }
        }

        private bool m_isStatic = false;

        public Block(Texture2D texture, GridPosition gridPosition)
        {
            m_texture = texture;
            m_gridPosition = gridPosition;
        }

        public enum MovementDirection
        {
            RIGHT,
            LEFT,
            DOWN
        }

        public void DetachFromTetrisBlock()
        {
            m_isStatic = true;
        }

        public bool IsStatic()
        {
            return m_isStatic;
        }

        public void Update(GameTime gameTime, BlockGrid blockGrid)
        {
            Move(blockGrid, MovementDirection.DOWN);
        }

        public bool CanMove(BlockGrid blockGrid, MovementDirection direction)
        {
            GridPosition desiredDirection = GridPosition.Zero;
            switch (direction)
            {
                case MovementDirection.DOWN:
                    desiredDirection = new GridPosition(0, 1);
                    break;
                case MovementDirection.RIGHT:
                    desiredDirection = new GridPosition(1, 0);
                    break;
                case MovementDirection.LEFT:
                    desiredDirection = new GridPosition(-1, 0);
                    break;
            }

            GridPosition desiredPosition = m_gridPosition + desiredDirection;
            // can't go outside the grid
            if (!blockGrid.ValidGridPosition(desiredPosition))
                return false;
            // can't occupy the same square as another static block
            else if (blockGrid.ContainsStaticBlock(desiredPosition))
                return false;
            else
                return true;
        }

        public void Move(BlockGrid blockGrid, MovementDirection direction)
        {
            if(CanMove(blockGrid, direction))
            {
                blockGrid.SetCell(m_gridPosition, null);
                switch (direction)
                {
                    case MovementDirection.DOWN:
                        m_gridPosition += new GridPosition(0, 1);
                        break;
                    case MovementDirection.LEFT:
                        m_gridPosition += new GridPosition(-1, 0);
                        break;
                    case MovementDirection.RIGHT:
                        m_gridPosition += new GridPosition(1, 0);
                        break;
                }

                blockGrid.SetCell(m_gridPosition, this);
            }
        }

        public static int CompareBlocksLeftToRight(Block b1, Block b2)
        {
            return b1.gridPosition.col - b2.gridPosition.col;
        }

        public static int CompareBlocksRightToLeft(Block b1, Block b2)
        {
            return b2.gridPosition.col - b1.gridPosition.col;
        }

        public static int CompareBlocksDownToUp(Block b1, Block b2)
        {
            return b2.gridPosition.row - b1.gridPosition.row;
        }
    }
}
